package com.titaniume.java2;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.*;

/**
 *@program: java8
 *@description:
 *@author: renfei.huang
 *@create: 2021-11-01 11:52
 */
public class TestSimpleDateFormat {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

//        Callable<Date> task = new Callable<Date>() {
//            @Override
//            public Date call() throws Exception {
//                //Caused by: java.lang.NumberFormatException: For input string: ".112021E4." 多线程问题
//                return sdf.parse("20211101");
//            }
//        };


        //解决多线程安全问题
      /*  Callable<Date> task = new Callable<Date>() {
            @Override
            public Date call() throws Exception {
                return DateFormatThreadLocal.convert("20211101");
            }
        };


        ExecutorService executorService = Executors.newFixedThreadPool(10);

        ArrayList<Future<Date>> futures = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            futures.add(executorService.submit(task));
        }

        for (Future<Date> future : futures) {
            System.out.println(future.get());
        }

        executorService.shutdown();*/



        //java8 方式 （没有多线程问题）
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");

        Callable<LocalDate> task = new Callable<LocalDate>() {


            @Override
            public LocalDate call() throws Exception {
                return LocalDate.parse("20211101",dtf);
             }
        };


        ExecutorService executorService = Executors.newFixedThreadPool(10);

        ArrayList<Future<LocalDate>> futures = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            futures.add(executorService.submit(task));
        }

        for (Future<LocalDate> future : futures) {
            System.out.println(future.get());
        }

        executorService.shutdown();



    }
}
